package com.mjfeed.activity;

import com.mjfeed.bean.MemberJSON;
import com.mjfeed.service.MemberService;
import com.mjfeed.util.NetUtils;
import com.mjfeed.util.SPUtils;
import com.mjfeed.widget.LoadingDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 注册登陆
 * @author zhaominglei
 * @date 2014-12-8
 * 
 */
public class RegisterLoginActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = RegisterLoginActivity.class.getSimpleName();
	
	private ImageView goHome;
	private Button register;
//	private TextView findpwd;
	private EditText accountText;
	private EditText passText;
	private Button login;
	private LoadingDialog loadingDialog;
	private String referer; // 来自那个链接
	private String username; //用户名
	private String pass; //密码
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_login);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.register_login_gohome);
		accountText = (EditText) findViewById(R.id.register_login_account_edt);
		passText = (EditText) findViewById(R.id.register_login_pass_edt);
		register = (Button) findViewById(R.id.register_login_register);
//		findpwd = (TextView) findViewById(R.id.register_login_findpwd);
		login = (Button) findViewById(R.id.register_login_login);
		
		goHome.setOnClickListener(this);
		register.setOnClickListener(this);
		login.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		
		referer = getIntent().getStringExtra("referer");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.register_login_gohome:
			finish();
			break;

		case R.id.register_login_register:
			intent = new Intent(RegisterLoginActivity.this, RegisterActivity.class);
			startActivity(intent);
			break;
			
		case R.id.register_login_login:
			login(v);
			break;
			
		default:
			break;
		}
	}

	/**
	 * 登录
	 */
	private void login(View v) {
		login.setClickable(false);
		username = accountText.getText().toString();
		pass = passText.getText().toString();
		
		if (username == null || username.equals("")) {
			login.setClickable(true);
			Toast.makeText(getApplicationContext(), R.string.register_login_check_error1, Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (pass == null || pass.equals("")) {
			login.setClickable(true);
			Toast.makeText(getApplicationContext(), R.string.register_login_check_error2, Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new LoginTask().execute("");
		} else {
			login.setClickable(true);
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}
	
	
	public class LoginTask extends AsyncTask<String, String, MemberJSON> {

		@Override
		protected MemberJSON doInBackground(String... params) {
			return new MemberService().login(getApplicationContext(), referer, username, pass);
		}

		@Override
		protected void onPostExecute(MemberJSON result) {
			if (result == null) {
				Toast.makeText(getApplicationContext(), R.string.register_login_error, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(), R.string.register_login_success, Toast.LENGTH_SHORT).show();
			}
			SPUtils.put(getApplicationContext(), "member", result);
			intent = new Intent(getApplicationContext(), MainActivity.class);
			intent.putExtra("referer", "RegisterLoginActivity");
			startActivity(intent);
			RegisterLoginActivity.this.finish();
			loadingDialog.dismiss();
		}
	}
}
